import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:schoolassets/%E5%85%A5%E5%BA%93/EntryKu.dart';
import 'package:schoolassets/%E6%9F%A5%E7%9C%8B/Search.dart';
import 'package:schoolassets/%E7%99%BB%E5%BD%95/Login.dart';
import 'package:schoolassets/%E7%9B%98%E7%82%B9/%E7%9B%98%E7%82%B9%E8%AE%A1%E5%88%92%E5%88%97%E8%A1%A8/InventoryList.dart';
import 'package:schoolassets/%E9%A2%86%E7%94%A8/%E7%89%A9%E8%B5%84%E9%A2%86%E7%94%A8%E5%8D%95/LingYongList.dart';
import 'package:schoolassets/Tools%E5%B7%A5%E5%85%B7/ShowToast.dart';
/**
 * Creat For 11643
 * CreatTime 2022/8/11
 * Description:首页
 */
class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List HomeList = [
    {"Name": "入库", "ImgUrl": "bangding","id":0},
    // {"Name": "借用", "ImgUrl": "ruku","id":1},
    {"Name": "领用", "ImgUrl": "chuku","id":2},
    // {"Name": "维修", "ImgUrl": "weixiu","id":3},
    // {"Name": "归还", "ImgUrl": "guihuan","id":4},
    {"Name": "盘点", "ImgUrl": "pandian","id":5},
    {"Name": "查看", "ImgUrl": "Search","id":6},
  ];
  //个人信息
  var UserMessage;
  //权限账号名称
  var PowerName;
  int last = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Builder(
          builder: (context) => Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.blue,
              title: Text("固定资产管理系统",style: TextStyle(fontSize: 15),),
              centerTitle: true,
              elevation: 0,
              actions: [
                IconButton(onPressed: (){
                  //退出APP
                  ExitApp();
                }, icon: Text("退出",style: TextStyle(fontSize: 15,color: Colors.white),))
              ],
            ),
            body: WillPopScope(
              child: Container(
                child: Column(
                  children: [
                    Container(
                      height: 170,
                      color: Colors.blue,
                      child: Center(
                        child: Column(
                          children: [
                            CircleAvatar(
                              radius: 50,
                              backgroundImage:
                              AssetImage("Image/touxiang.png"),
                            ),
                            Container(
                              height: 5,
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 15),
                              child: Text("Admin",style: TextStyle(fontSize: 25,color: Colors.white),),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      height: 30,
                    ),
                    Expanded(
                      child: GridView.builder(
                        gridDelegate:
                        SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount:2,
                            crossAxisSpacing: 0.3,
                            mainAxisSpacing:0.5,
                            childAspectRatio: 1.0),
                        itemCount: HomeList.length,
                        itemBuilder: (BuildContext context, int index) {
                          return GestureDetector(
                            child: Container(
                              // margin: EdgeInsets.only(top: 15),
                              // padding: EdgeInsets.all(15),
                              child: Center(
                                child: Column(
                                  children: [
                                    // 图标
                                    Container(
                                      child: Image.asset(
                                        "Image/${HomeList[index]['ImgUrl']}.png",
                                        width: 70,
                                        height: 70,
                                      ),
                                    ),
                                    Container(
                                      height: 5,
                                    ),
                                    // 功能名称
                                    Container(
                                      child: Text(
                                        "${HomeList[index]['Name']}",
                                        style: TextStyle(fontSize: 17),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                            onTap: () {
                              if (HomeList[index]['id'] == 0) {
                                log("入库");
                                //入库
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => EntryKu()));
                              } else if (HomeList[index]['id'] == 1){
                                  //借用
                              }else if (HomeList[index]['id'] == 2){
                                  // 领用
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => LingYongList()));
                              }else if (HomeList[index]['id'] == 3){
                                  // 维修
                              }else if (HomeList[index]['id'] == 4){
                                  // 归还
                              }else if (HomeList[index]['id'] == 5){
                                  // 盘点
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => InventoryList()));
                              }else{
                                // 查看
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => Search()));
                              }
                            },
                          );
                        },
                      ),
                    )
                  ],
                ),
              ),
              onWillPop: () async {
                //计算两次时间间隔
                int now = DateTime.now().millisecondsSinceEpoch;
                if (now - last > 3000) {
                  last = DateTime.now().millisecondsSinceEpoch;
                  ShowToast().MyToast('再次点击返回退出应用');
                  return Future.value(false);
                } else {
                  return Future.value(true);
                }
              },
            ),
          )),
    );
  }
  //  退出账号
  void ExitApp(){
    //弹出提示框
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text('退出账号'),
          content: new SingleChildScrollView(
            child: new ListBody(
              children: <Widget>[
                new Text('您确定要退出账号吗？'),
              ],
            ),
          ),
          actions: <Widget>[
            new FlatButton(
              child: new Text('确定'),
              onPressed: () async {
                Navigator.of(context).pop();
                //  关闭所有页面，进入登陆页面
                Future.delayed(Duration.zero,()  {
                  ///跳转到起始页并关闭所有页面
                  Navigator.pushAndRemoveUntil(context, new MaterialPageRoute(builder: (context) => new Login()), (route) => route == null);
                });
              },
            ),
            new FlatButton(
              child: new Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }
}
